<?php
/**
*
* @package ppkBB3cker
* @version $Id: caddchatm.php 1.000 2010-03-14 13:14:00 PPK $
* @copyright (c) 2010 PPK
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
	exit;
}

if($rights[4] && $rights[2])//is_canviewchatm is_canaddchatm
{
	if($ppkbb_chat_autokill_onpost && !$rights[10] && ($dt - $chat_user['lastpost']) < $ppkbb_chat_waittime - 1)
	{
		userkick(2);
	}
	else
	{
		if(!$acommand)
		{
			//$chat_message = str_replace('---', '&minus;&minus;&minus;', $chat_message);
			//$chat_user['username'] = str_replace('---', '&minus;&minus;&minus;', $chat_user['username']);
			intval($ppkbb_chat_messlength) ? '' : $ppkbb_chat_messlength=255;
			$mbstring=extension_loaded('mbstring') ? 1 : 0;
			$chat_message_len=$mbstring ? mbstring_utf8_strlen($chat_message) : utf8_strlen($chat_message);

			if($chat_message_len > $ppkbb_chat_messlength)
			{
				$chat_message=$mbstring ? mbstring_utf8_substr($chat_message, 0, $ppkbb_chat_messlength) : utf8_substr($chat_message, 0, $ppkbb_chat_messlength);
			}

			if($to_user_id && ((@!$users[$to_user_id] && !$rights[14])/* || @$users[$to_user_id]['user_hidden']*/ || @$users[$to_user_id]['lastaccess'] > $dt))
			{
				$to_user_id=0;
			}
			else
			{
				$get_gmode && $to_room && $to_user_id ? $to_room=$forum_id : '';
				!$ppkbb_chat_enable_bbcodes || !$rights[12] ? $chat_message=preg_replace('#\[/?(b|i|u|pre|samp|code|colou?r(=[^\[]+)?|size(=[^\[]+)?|noparse|url(=[^\[]+)?|s|q|blockquote)\]#', '', $chat_message) : '';
				!$ppkbb_chat_enable_bbcodes || !$rights[17] ? $chat_message=preg_replace('#\[/?img(=[^\[]+)?\]#', '', $chat_message) : '';
				if($chat_message!=='')
				{
					$sql='INSERT INTO ' .PPKCHAT_MESSAGES_TABLE. " (message, user_id, to_user, date, rights, room, username, user_color, user_ip) VALUES ('{$chat_message}', '{$chat_user['user_id']}', '".($rights[9] && $to_user_id ? $to_user_id : 0)."', '{$dt}', '{$chat_user['rights']}', '".($get_gmode && $to_room ? $to_room : $forum_id)."', '{$chat_user['username']}', '{$chat_user['user_color']}', INET_ATON('".mysql_real_escape_string(@$_SERVER['REMOTE_ADDR'], $c)."'))";
					$result=my_sql_query($sql);
					$lastpost=$dt;
					$skip_lacheck=1;
				}
			}
		}
	}
}
else
{
	userkick(3);
}

//############################################################

//From includes/utf/utf_tools.php
function mbstring_utf8_substr($str, $offset, $length = null)
{
	if (is_null($length))
	{
		return mb_substr($str, $offset);
	}
	else
	{
		return mb_substr($str, $offset, $length);
	}
}

//From includes/utf/utf_tools.php
function mbstring_utf8_strlen($text)
{
	return mb_strlen($text, 'utf-8');
}

//From includes/utf/utf_tools.php
function utf8_substr($str, $offset, $length = NULL)
{
	// generates E_NOTICE
	// for PHP4 objects, but not PHP5 objects
	$str = (string) $str;
	$offset = (int) $offset;
	if (!is_null($length))
	{
		$length = (int) $length;
	}

	// handle trivial cases
	if ($length === 0 || ($offset < 0 && $length < 0 && $length < $offset))
	{
		return '';
	}

	// normalise negative offsets (we could use a tail
	// anchored pattern, but they are horribly slow!)
	if ($offset < 0)
	{
		// see notes
		$strlen = utf8_strlen($str);
		$offset = $strlen + $offset;
		if ($offset < 0)
		{
			$offset = 0;
		}
	}

	$op = '';
	$lp = '';

	// establish a pattern for offset, a
	// non-captured group equal in length to offset
	if ($offset > 0)
	{
		$ox = (int) ($offset / 65535);
		$oy = $offset % 65535;

		if ($ox)
		{
			$op = '(?:.{65535}){' . $ox . '}';
		}

		$op = '^(?:' . $op . '.{' . $oy . '})';
	}
	else
	{
		// offset == 0; just anchor the pattern
		$op = '^';
	}

	// establish a pattern for length
	if (is_null($length))
	{
		// the rest of the string
		$lp = '(.*)$';
	}
	else
	{
		if (!isset($strlen))
		{
			// see notes
			$strlen = utf8_strlen($str);
		}

		// another trivial case
		if ($offset > $strlen)
		{
			return '';
		}

		if ($length > 0)
		{
			// reduce any length that would
			// go passed the end of the string
			$length = min($strlen - $offset, $length);

			$lx = (int) ($length / 65535);
			$ly = $length % 65535;

			// negative length requires a captured group
			// of length characters
			if ($lx)
			{
				$lp = '(?:.{65535}){' . $lx . '}';
			}
			$lp = '(' . $lp . '.{'. $ly . '})';
		}
		else if ($length < 0)
		{
			if ($length < ($offset - $strlen))
			{
				return '';
			}

			$lx = (int)((-$length) / 65535);
			$ly = (-$length) % 65535;

			// negative length requires ... capture everything
			// except a group of  -length characters
			// anchored at the tail-end of the string
			if ($lx)
			{
				$lp = '(?:.{65535}){' . $lx . '}';
			}
			$lp = '(.*)(?:' . $lp . '.{' . $ly . '})$';
		}
	}

	if (!preg_match('#' . $op . $lp . '#us', $str, $match))
	{
		return '';
	}

	return $match[1];
}
?>

